package com.wangcp.utils;

import com.wangcp.entity.KeyValueForDate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 时间分割工具类
 */
public class SplitDateUtil {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public static List<KeyValueForDate> getKeyValueForDate(String startDate, String endDate) throws ParseException {
        List<KeyValueForDate> list = new ArrayList<>();
        // 定义每月开始时间于结束时间
        String firstDay = "";
        String lastDay = "";
        // 获取传入的开始时间于结束时间并转为Date
        Date stDate = sdf.parse(startDate);
        Date enDate = sdf.parse(endDate);

        // 定义日期实例
        Calendar startCale = Calendar.getInstance();
        startCale.setTime(stDate);

        Calendar endCale = Calendar.getInstance();
        endCale.setTime(enDate);

        long startDayLong = stDate.getTime();
        long endDayLong = enDate.getTime();

        Calendar cale = Calendar.getInstance();
        KeyValueForDate keyValueForDate = null;
        // 当当前时间小于结束时间
        while (startCale.getTime().before(enDate)){
            keyValueForDate = new KeyValueForDate();
            cale.setTime(startCale.getTime());

            // 如果当前时间与开始时间相同
            if(startCale.getTime().equals(stDate)){
                // 获取开始月的最后一天为该月结束时间
                cale.set(Calendar.DAY_OF_MONTH,startCale.getActualMaximum(Calendar.DAY_OF_MONTH));
                lastDay = sdf.format(cale.getTime());
                keyValueForDate.setStartDate(sdf.format(stDate));
                keyValueForDate.setEndDate(lastDay);
            }else if(startCale.get(Calendar.MONTH) == enDate.getMonth() && startCale.get(Calendar.YEAR) == endCale.get(Calendar.YEAR)){
                cale.set(Calendar.DAY_OF_MONTH,1);
                firstDay = sdf.format(cale.getTime());

                keyValueForDate.setStartDate(firstDay);
                keyValueForDate.setEndDate(endDate);
            }else{
                // 获取该月份的第一天做为该月开始时间
                cale.set(Calendar.DAY_OF_MONTH,1);
                firstDay = sdf.format(cale.getTime());

                cale.set(Calendar.DAY_OF_MONTH,startCale.getActualMaximum(Calendar.DAY_OF_MONTH));
                lastDay = sdf.format(cale.getTime());

                keyValueForDate.setStartDate(firstDay);
                keyValueForDate.setEndDate(lastDay);
            }
            list.add(keyValueForDate);
            // 当前时间月份加1
            startCale.add(Calendar.MONTH,1);
        }
        if(endDayLong < startDayLong){
            keyValueForDate = new KeyValueForDate();

            cale.setTime(enDate);
            cale.set(Calendar.DAY_OF_MONTH,1);
            firstDay = sdf.format(cale.getTime());

            keyValueForDate.setStartDate(firstDay);
            keyValueForDate.setEndDate(sdf.format(enDate));
            list.add(keyValueForDate);
        }
        return list;
    }


    public static void main(String[] args) throws ParseException {
        List<KeyValueForDate> keyValueForDate = getKeyValueForDate("2015-08-03", "2016-06-10");
        System.out.println("开始时间----------------结束时间");
        for (KeyValueForDate valueForDate : keyValueForDate) {
            System.out.println(valueForDate.getStartDate() + "-------------------" + valueForDate.getEndDate());
        }
    }

}
